﻿using _1_Interfaces.Interfaces.Cjw;
using _1_Interfaces.Models.Dtos.Cjw;
using _1_Interfaces.Models.Tables.Cjw;
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace _2_Services.Services.Cjw
{
    /// <summary>
    /// 科室病床表服务
    /// </summary>
    public class SickBedListService : ISickBedListService
    {
        SqlSugarClient _db = CjwDbContext.GetInstance();
        private IMapper _mapper;

        public SickBedListService(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 获取病床列表
        /// </summary>
        /// <param name="sickBedListDto"></param>
        /// <returns></returns>
        public SickBedListOutDto GetSickBedList(SickBedListDto sickBedListDto)
        {
            SickBedListOutDto sickBedListOutDto = new SickBedListOutDto();
            int totalCount= 0;
            sickBedListOutDto.sickBedLists = _db.Queryable<SickBedList>()
                .Where(x => x.DepartmentId == sickBedListDto.DepartmentId)
                .WhereIF(sickBedListDto.SickBedId != 0, x => x.SickBedId == sickBedListDto.SickBedId)
                .WhereIF(sickBedListDto.SickBedName != "", x => x.SickBedName.Contains(sickBedListDto.SickBedName))
                .WhereIF(sickBedListDto.SickBedState != -1, x => x.SickBedState == sickBedListDto.SickBedState)
                .ToPageList(sickBedListDto.PageIndex, sickBedListDto.PageSize, ref totalCount);

            sickBedListOutDto.Total = totalCount;
            //if (totalCount >= sickBedListDto.PageSize)
            //{
            //    sickBedListOutDto.TotalPage = totalCount % sickBedListDto.PageSize == 0 ? totalCount / sickBedListDto.PageSize : totalCount / sickBedListDto.PageSize + 1;
            //}
            //else
            //{
            //    sickBedListOutDto.TotalPage = 1;
            //}
            sickBedListOutDto.TotalPage= Tool.PageCount(totalCount, sickBedListDto.PageSize);
            return sickBedListOutDto;
        }

        /// <summary>
        /// 新增病床
        /// </summary>
        /// <param name="sickBedListAddDto"></param>
        /// <returns></returns>
        public bool InsertSickBed(SickBedListAddDto sickBedListAddDto)
        {
            SickBedList sickBedList = _mapper.Map<SickBedList>(sickBedListAddDto);
            if (_db.Insertable(sickBedList).ExecuteCommand() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改病床
        /// </summary>
        /// <param name="sickBedList"></param>
        /// <returns></returns>
        public bool UpdateSickBed(SickBedList sickBedList)
        {
            return _db.Updateable(sickBedList).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 病床状态修改
        /// </summary>
        /// <param name="sickBedListStateDto"></param>
        /// <returns></returns>
        public bool UpdateBedState(SickBedListStateDto sickBedListStateDto)
        {
            return _db.Updateable<SickBedList>()
                .SetColumns(x => x.SickBedState == sickBedListStateDto.SickBedState)
                .Where(x => sickBedListStateDto.SickBedIds.Contains(x.SickBedId))
                .ExecuteCommandHasChange();
        }

        /// <summary>
        /// 删除病床
        /// </summary>
        /// <param name="sickBedIds"></param>
        /// <returns></returns>
        public bool DeleteSickBed(List<int> sickBedIds)
        {
            return _db.Deleteable<SickBedList>()
                .Where(x => sickBedIds.Contains(x.SickBedId))
                .ExecuteCommandHasChange();
        }

        /// <summary>
        /// 小程序端的亲戚查询病床
        /// </summary>
        /// <param name="patientName">病人姓名</param>
        /// <param name="phoneNumber">病人电话号码</param>
        /// <returns></returns>
        public SickBedAppSearchDto AppSearch(string patientName, string phoneNumber)
        {
            return _db.Queryable<PatientList>()
                .LeftJoin<PatientRecordList>((p, pr) => p.PatientId == pr.PatientId)
                .InnerJoin<DepartmentList>((p, pr, d) => pr.DepartmentId == d.DepartmentId)
                .InnerJoin<SickBedList>((p, pr, d, s) => pr.SickBedId == s.SickBedId)
                .Where((p, pr, d, s) => pr.State == 5)
                .WhereIF(patientName != null, (p, pr, d, s) => p.PatientName.Contains(patientName))
                .WhereIF(phoneNumber != null, (p, pr, d, s) => p.PhoneNumber.Equals(phoneNumber))
                .Select((p, pr, d, s) => new SickBedAppSearchDto
                {
                    PatientName = p.PatientName,
                    PhoneNumber = p.PhoneNumber,
                    Sex = p.Sex,
                    DepartmentId = d.DepartmentId,
                    DepartmentName = d.DepartmentName,
                    SickBedId = s.SickBedId,
                    SickBedName = s.SickBedName
                })
                .First();
        }
    }
}
